﻿using LD.Host;
using LD.Host.Mes;
using LD.Model;
using LD.Tool;
using System;
using System.Collections;
using System.Threading.Tasks;
using 右下角弹窗.DiagShow;

namespace LD.MES
{
    public partial class FrmMesByHYFSKScrew : BaseFrm
    {
        public FrmMesByHYFSKScrew()
        {
            InitializeComponent();
            this.Shown += MesHelperSharpFrm_Shown;
        }

        private void MesHelperSharpFrm_Shown(object sender, EventArgs e)
        {
            base.Permission(UserType.操作工);

            if (this.IsDisposed)
                return;
            Init();
        }
        private void Init()
        {
            foreach (var item in CommunicateFactory.Instance.TcpIp)
            {
                this.cob_tcpName.Items.Add(item.Key);
            }
            if (Cache.GetCache(CONST.HYFSK_DATA_DAY) == null)
                Cache.SetCache(CONST.HYFSK_DATA_DAY, 30);

            if (Cache.GetCache(CONST.SYS_SOURCE_RUN) == null)
                Cache.SetCache(CONST.SYS_SOURCE_RUN, false);

            this.txt_stationId.Text = (string)Cache.GetCache(CONST.HYFSK_MES_STATIONNUM);
            this.txt_port.Text = (string)Cache.GetCache(CONST.HYFSK_MES_INTERFACE);
            this.txt_stationCode.Text = (string)Cache.GetCache(CONST.HYFSK_MES_STATIONCODE);
            this.txt_mesUrl.Text = (string)Cache.GetCache(CONST.HYFSK_MES_URL);
            this.txt_slot1.Text = (string)Cache.GetCache(CONST.HYFSK_SCREW_SLOT1) + "";
            this.txt_slot2.Text = (string)Cache.GetCache(CONST.HYFSK_SCREW_SLOT2) + "";
            this.sw_sourceRun.Checked = (bool)Cache.GetCache(CONST.SYS_SOURCE_RUN);
            this.txt_dataDay.Text = (int)Cache.GetCache(CONST.HYFSK_DATA_DAY) + "";
            this.cob_tcpName.Text = (string)Cache.GetCache(CONST.HYFSK_TCPNAME);//螺丝机临时领用变量
        }
        public void Save()
        {
            Cache.SetCache(CONST.HYFSK_MES_STATIONNUM, this.txt_stationId.Text);
            Cache.SetCache(CONST.HYFSK_MES_STATIONCODE, this.txt_stationCode.Text);
            Cache.SetCache(CONST.HYFSK_MES_INTERFACE, this.txt_port.Text);
            Cache.SetCache(CONST.HYFSK_MES_URL, this.txt_mesUrl.Text);
            Cache.SetCache(CONST.SYS_SOURCE_RUN, this.sw_sourceRun.Checked);
            Cache.SetCache(CONST.HYFSK_SCREW_SLOT1, this.txt_slot1.Text);
            Cache.SetCache(CONST.HYFSK_SCREW_SLOT2, this.txt_slot2.Text);
            Cache.SetCache(CONST.HYFSK_TCPNAME, this.cob_tcpName.Text);//螺丝机临时领用变量

            if (int.TryParse(this.txt_dataDay.Text, out var dataDay))
                Cache.SetCache(CONST.HYFSK_DATA_DAY, dataDay);
            Dialog dialog = new Dialog();
            dialog.Success("保存成功");
        }

        private void btn_upload_Click(object sender, EventArgs e)
        {
            string machine = (string)Cache.GetCache(CONST.DPT_MACHINE);
            Hashtable table = new Hashtable
            {
                { "station_num", this.txt_stationId.Text },
                { "station_code", this.txt_stationCode.Text },
                { "wip_no", this.txt_barcode.Text }
            };
            bool result = MesByHYFSK.Check(this.txt_mesUrl.Text, this.txt_port.Text, table);
            //if (result)
            //    this.txt_result.Text = "上传成功";
            //else
            //    this.txt_result.Text = "上传失败";
        }

        private void btn_upkazam_Click(object sender, EventArgs e)
        {

            string barcode = this.txt_barcode.Text;

            if (!string.IsNullOrEmpty(barcode))
            {
                string cmd = $"CMD=StartOne|Slot={1}|SN={barcode}";
                cmd = cmd + "\r\n";
                KazamService.Ins.ClientSendMulti(this.cob_tcpName.Text, $"Slot{1}", cmd);
                Task.Delay(3000).Wait();
                string cmd2 = $"|Result=PASS|Slot={1}";
                cmd2 = cmd2 + "\r\n";
                KazamService.Ins.ServiceSend_Data($"Slot{1}", cmd2);
            }
        }
    }
}
